/**
 * @Title: UserModal.js
 * @Description: UserModal
 * @author: Fanchao
 * @date: 2020/8/1 2:49 下午
 * @version V1.0
 */
import React, { memo } from "react";
import { connect } from "umi";
import { Form, Input, Modal, Select, Cascader } from "antd";

const layout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 18 }
};

const { Option } = Select;
const UserModal = memo(({ teachers: { itemData, treeData }, dispatch, title, visible }) => {
    const [form] = Form.useForm();
    form.setFieldsValue({
        realName: itemData.teacherRealName,
        loginId: itemData.loginId,
        credentialsNo: itemData.credentialsNo,
        teacherTitleType: itemData.teacherTitleType,
        departmentSupervisionExpert: itemData.teacherUserId ? itemData.departmentSupervisionExpert ? "1" : "0" : "",
        schoolSupervisionExpert: itemData.teacherUserId ? itemData.schoolSupervisionExpert ? "1" : "0" : "",
        provinceFamousTeacher: itemData.teacherUserId ? itemData.provinceFamousTeacher ? "1" : "0" : "",
        corpsFamousTeacher: itemData.teacherUserId ? itemData.corpsFamousTeacher ? "1" : "0" : ""
    });
    return (
        <Modal
            title={title}
            visible={visible}
            width="900px"
            onOk={() => {
                form.validateFields().then(values => {
                    if (itemData.teacherUserId) {
                        values.teacherUserId = itemData.teacherUserId;
                        values.teacherUserName = values.realName;
                        values.corpsFamousTeacher = values.corpsFamousTeacher === "1" ? true : false;
                        values.provinceFamousTeacher = values.provinceFamousTeacher === "1" ? true : false;
                        values.schoolSupervisionExpert = values.schoolSupervisionExpert === "1" ? true : false;
                        values.departmentSupervisionExpert = values.departmentSupervisionExpert === "1" ? true : false;
                        delete values.realName;
                        dispatch({
                            type: "teachers/updateTeacherUserInfo",
                            payload: values,
                            callback: (result) => {
                                if (result) {
                                    form.resetFields();
                                    dispatch({
                                        type: "teachers/setItemData",
                                        payload: {}
                                    });
                                    dispatch({
                                        type: "teachers/setModalVisible",
                                        payload: false
                                    });
                                    dispatch({
                                        type: "teachers/getDataSource",
                                        payload: {
                                            keyword: "",
                                            reqPageInfo: {
                                                pageNumber: 1,
                                                pageSize: 10
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    } else {
                        values.userRoleType = "Teacher";
                        values.corpsFamousTeacher = values.corpsFamousTeacher === "1" ? true : false;
                        values.provinceFamousTeacher = values.provinceFamousTeacher === "1" ? true : false;
                        values.schoolSupervisionExpert = values.schoolSupervisionExpert === "1" ? true : false;
                        values.departmentSupervisionExpert = values.departmentSupervisionExpert === "1" ? true : false;
                        values.leastDepartmentId = values.leastDepartmentId.pop();
                        dispatch({
                            type: "teachers/addTeacher",
                            payload: values,
                            callback: (result) => {
                                if (result) {
                                    form.resetFields();
                                    dispatch({
                                        type: "teachers/setItemData",
                                        payload: {}
                                    });
                                    dispatch({
                                        type: "teachers/setModalVisible",
                                        payload: false
                                    });
                                    dispatch({
                                        type: "teachers/getDataSource",
                                        payload: {
                                            keyword: "",
                                            reqPageInfo: {
                                                pageNumber: 1,
                                                pageSize: 10
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                }).catch(info => {
                });
            }}
            onCancel={() => {
                form.resetFields();
                dispatch({
                    type: "teachers/setItemData",
                    payload: {}
                });
                dispatch({
                    type: "teachers/setModalVisible",
                    payload: false
                });
            }}
        >
            <Form
                {...layout}
                layout="horizontal"
                form={form}
                name="basic"
                // 表单默认值，只有初始化以及重置时生效 莫用
                initialValues={{
                    // name: itemData.name
                }}
            >
                <Form.Item
                    label="教员姓名"
                    name="realName"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Input placeholder="请输入教员姓名"/>
                </Form.Item>
                <Form.Item
                    label="登录账号"
                    name="loginId"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Input placeholder="请输入登录账号"/>
                </Form.Item>
                {
                    itemData.teacherUserId ? null : (
                        <Form.Item
                            label="登录密码"
                            name="password"
                            rules={[{ required: true, message: "必填" }]}
                        >
                            <Input placeholder="请输入登录密码"/>
                        </Form.Item>
                    )
                }
                <Form.Item
                    label="证件号"
                    name="credentialsNo"
                    // rules={[{ required: true, message: "必填" }]}
                >
                    <Input placeholder="请输入证件号"/>
                </Form.Item>
                <Form.Item
                    label="教员职称"
                    name="teacherTitleType"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Select
                        placeholder="请选择职称"
                    >
                        <Option value="professor">教授</Option>
                        <Option value="associate_professor">副教授</Option>
                        <Option value="instructor">讲师</Option>
                        <Option value="assistant">助教</Option>
                    </Select>
                </Form.Item>
                <Form.Item
                    label="部系专家"
                    name="departmentSupervisionExpert"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Select>
                        <Option value="1">是</Option>
                        <Option value="0">否</Option>
                    </Select>
                </Form.Item>
                <Form.Item
                    label="学校级教学督导专家"
                    name="schoolSupervisionExpert"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Select>
                        <Option value="1">是</Option>
                        <Option value="0">否</Option>
                    </Select>
                </Form.Item>
                <Form.Item
                    label="省级以上教学名师"
                    name="provinceFamousTeacher"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Select>
                        <Option value="1">是</Option>
                        <Option value="0">否</Option>
                    </Select>
                </Form.Item>
                <Form.Item
                    label="军级以上教学名师"
                    name="corpsFamousTeacher"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Select>
                        <Option value="1">是</Option>
                        <Option value="0">否</Option>
                    </Select>
                </Form.Item>
                {
                    itemData.teacherUserId ? null : (
                        <Form.Item
                            label="所在单位"
                            name="leastDepartmentId"
                            rules={[{ required: true, message: "必填" }]}
                        >
                            <Cascader
                                changeOnSelect
                                options={treeData}
                            />
                        </Form.Item>
                    )
                }

            </Form>
        </Modal>
    );
});

export default connect(({ teachers, dispatch }) => ({
    teachers,
    dispatch
}))(UserModal);
